package com.longxi.gamer_base.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longxi.gamer_base.R;
import com.longxi.gamer_base.listener.WarnEditListener;

public class WarnEditDialog extends Dialog {
    public WarnEditDialog(@NonNull Context context) {
        super(context);
    }

    public WarnEditDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected WarnEditDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String message;//提示消息
        private String hint;//输入框提示
        private String content;//输入框内容
        private String negative_text;//消极的
        private String positive_text;//积极的
        private OnClickListener negativeListener;//消极的监听
        private WarnEditListener positiveListener;//积极的监听

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            if (message == null) {
                this.message = "您没有填写提示信息哦";
            }
            this.message = message;
            return this;
        }

        public Builder setHint(String hint) {
            if (hint == null) {
                this.hint = "您没有填写输入提示信息哦";
            }
            this.hint = hint;
            return this;
        }

        public Builder setContent(String content) {
            if (content == null) {
                this.content = "";
            }
            this.content = content;
            return this;
        }

        public Builder setNegativeButton(String negative_text, OnClickListener negativeListener) {
            if (negative_text == null) {
                this.negative_text = "取消";
            }
            this.negative_text = negative_text;
            this.negativeListener = negativeListener;

            return this;
        }

        public Builder setPositionButton(String positive_text, WarnEditListener positiveListener) {
            if (positive_text == null) {
                this.positive_text = "确定";
            }
            this.positive_text = positive_text;
            this.positiveListener = positiveListener;

            return this;
        }

        private TextView tv_message_custom_dialog;//提示信息
        private EditText et_reason;
        private Button btn_negative_custom_dialog;//消极
        private Button btn_positive_custom_dialog;//积极


        public WarnEditDialog create() {
            final WarnEditDialog dialog = new WarnEditDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.base_dialog_warn_edit, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
//      dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_message_custom_dialog = (TextView) view.findViewById(R.id.base_tv_message_custom_dialog);
            et_reason = (EditText) view.findViewById(R.id.base_et_reason);
            btn_negative_custom_dialog = (Button) view.findViewById(R.id.base_btn_negative_custom_dialog);
            btn_positive_custom_dialog = (Button) view.findViewById(R.id.base_btn_positive_custom_dialog);
            tv_message_custom_dialog.setText(message);
            et_reason.setHint(hint);
            et_reason.setText(content);
            btn_negative_custom_dialog.setText(negative_text);
            btn_positive_custom_dialog.setText(positive_text);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            btn_negative_custom_dialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                }
            });
            btn_positive_custom_dialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if("".equals(et_reason.getText().toString())){
                        Toast.makeText(context, et_reason.getHint().toString().trim(), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    positiveListener.onClick(dialog, et_reason.getText().toString());
                }
            });
            return dialog;
        }
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        getWindow().setAttributes(layoutParams);
    }
}
